//
//  TipsView.m
//  iOSLib
//
//  Created by gaofeng on 15/11/24.
//  Copyright © 2015年 zyb. All rights reserved.
//

#import "TipsView.h"
#import "UIColor+Hex.h"
#import "UIView+Gesture.h"
#import "ZYBViewDefine.h"
#import "UIView+YYAdd.h"
#import "NSString+Additions.h"


@interface TipsView ()

@property (nonatomic, strong, readonly) UILabel *titleLabel;

@end

@implementation TipsView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor colorWithHex:0xf8f8f8];
        self.clipsToBounds = YES;
        self.userInteractionEnabled = YES;
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 44, 44)];
        button.backgroundColor = [UIColor clearColor];
        [button setImage:[UIImage imageNamed:@"nav_bar_back"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"nav_bar_back_pressed"] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(backButtonTouched) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        _backButton = button;
        _backButton.hidden = YES;
        
        UIImageView * tipImageView = [[UIImageView alloc] init];
        tipImageView.userInteractionEnabled = YES;
        [self addSubview:tipImageView];
        _imageView = tipImageView;
        
        UILabel * tipTitle = [[UILabel alloc] initWithFrame:CGRectMake(45, _imageView.bottom + 10, self.frame.size.width - 90, 15)];
        tipTitle.font = [UIFont systemFontOfSize:14];
        tipTitle.numberOfLines = 0;
        tipTitle.textAlignment = NSTextAlignmentCenter;
        tipTitle.textColor = [UIColor colorWithHex:0x666666];
        tipTitle.backgroundColor = [UIColor clearColor];
        [self addSubview:tipTitle];
        _titleLabel = tipTitle;
      
        UIButton *tipButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tipButton.frame = CGRectMake(0, CGRectGetMaxY(_titleLabel.frame) + 20, 130, 40);
        tipButton.layer.cornerRadius = 5;
        tipButton.layer.masksToBounds = YES;
        tipButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [tipButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        tipButton.backgroundColor = [UIColor colorWithHex:0x45B7FF];
        tipButton.center = CGPointMake(self.frame.size.width/2.0, tipButton.center.y);
        [tipButton addTarget:self action:@selector(tipButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:tipButton];
        _tipButton = tipButton;
    }
    
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _titleLabel.width = self.width - 90;
    [_titleLabel sizeToFit];
    _titleLabel.width = self.width - 90;
    
    [self resetSubviewsFrame];
    _tipButton.top = _titleLabel.bottom + 20;
    _tipButton.centerX = self.centerX;
}

- (void)resetSubviewsFrame {
    CGFloat diff = 15;
    BOOL hasText = _titleLabel.text.length > 0 || _titleLabel.attributedText.length > 0;
    if (_imageView.image && hasText) {
        CGFloat x = floorf((self.frame.size.width - _imageView.image.size.width) / 2);
        CGFloat y = floorf((self.frame.size.height - _imageView.image.size.height - diff - _titleLabel.height) / 2);
        _imageView.frame = CGRectMake(x, y, _imageView.image.size.width, _imageView.image.size.height);
        _titleLabel.top = _imageView.bottom + diff;
        return;
    }
    
    if (_imageView.image) {
        _imageView.frame = CGRectMake(0, 0, _imageView.image.size.width, _imageView.image.size.height);
         _imageView.center = CGPointMake(self.width / 2, self.height / 2);
        return;
    }
    
    if (hasText) {
        _titleLabel.top = floorf((self.height - _titleLabel.height) / 2);
    }
}

- (void)setModel:(TipsTextModel *)model{
    _model = model;
    if (model.tipButtonConfigure) {
        [_tipButton setTitleColor:model.tipButtonConfigure.textColor forState:UIControlStateNormal];
        _tipButton.titleLabel.font = model.tipButtonConfigure.font;
        _tipButton.backgroundColor = model.tipButtonConfigure.backgroundColor;
        _tipButton.layer.masksToBounds = YES;
        _tipButton.layer.cornerRadius = 5;
        _tipButton.layer.borderColor = model.tipButtonConfigure.boaderColor.CGColor;
        _tipButton.layer.borderWidth = model.tipButtonConfigure.boaderWidth;
        _tipButton.size = model.tipButtonConfigure.buttonSize;
        _tipButton.userInteractionEnabled = NO;
    }
    if (model.titleConfigure) {
        _titleLabel.font = model.titleConfigure.font;
        _titleLabel.textColor = model.titleConfigure.textColor;
        _titleLabel.backgroundColor = model.titleConfigure.backgroundColor;
    }
    if(model.tipsViewConfigure){
        self.backgroundColor = model.tipsViewConfigure.backgroundColor;
    }
    if(model.tipButtonTitle && model.tipButtonTitle.length) {
        [_tipButton setTitle:model.tipButtonTitle forState:UIControlStateNormal];
        _tipButton.hidden = NO;
    } else {
        _tipButton.hidden = YES;
    }
    _imageView.image = model.image;
    CGSize size = [model.title sizeWithMaxWidth:_titleLabel.width font:_titleLabel.font];
    _titleLabel.text = model.title;

    if ([model.title isKindOfClass:[NSAttributedString class]]) {
        NSAttributedString *attrTitle = (NSAttributedString *)model.title;
        size = [attrTitle size];
        _titleLabel.attributedText = model.title;
    }
    if (size.height>20) {
        _titleLabel.height = size.height;
    }
    [self setNeedsLayout];
}

- (void)tipButtonClicked:(id)sender {

    if (self.tipButtonTouchedBlock) {
        self.tipButtonTouchedBlock(_tipType);
    }
}

- (void)backButtonTouched {
    if (self.backButtonTouchedBlock) {
        self.backButtonTouchedBlock(self.tipType);
    }
}

@end
